﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using System.IO;

namespace Aim2.Screen {
    class MainMenuScreen : MenuScreen {
        enum MainMenuState {
            UNDEFINED,
            WELCOME,
            HIGHSCORES,
            SELECTLEVEL,
            OPTIONS
        }
        MainMenuState state;
        MainMenuState State {
            get { return state; }
            set {
                // exit early from trivial sets
                if (State == value) {
                    return;
                }
                state = value;
                if (MenuEntries != null) {
                    switch (State) {
                        case MainMenuState.WELCOME: {
                                MenuEntries.Clear();
                                MenuEntries.Add("Start");
                                MenuEntries.Add("Options");
                                MenuEntries.Add("Highscores");
                                MenuEntries.Add("Exit");
                                break;
                            }
                        case MainMenuState.HIGHSCORES: {
                                MenuEntries.Clear();
                                MenuEntries.Add("Back");
                                break;
                            }
                        case MainMenuState.SELECTLEVEL: {
                                MenuEntries.Clear();
                                foreach(DirectoryInfo d in Game.Instance.tracks)
                                    MenuEntries.Add(d.Name);
                                MenuEntries.Add("Back");
                                break;
                            }
                        case MainMenuState.OPTIONS: {
                                MenuEntries.Clear();
                                MenuEntries.Add("Back");
                                break;
                            }
                    }
                }
            }
        }

        public MainMenuScreen()
            : base() {
            // set the transition times
            TransitionOnTime = TimeSpan.FromSeconds(1.0);
            TransitionOffTime = TimeSpan.FromSeconds(0.0);
            State = MainMenuState.WELCOME;
            AudioManager.PlayMenuTrack();
            BackgroundScreen.Background.Bars.NumberOfBarsLayers = 3;
        }

        public override void Draw(GameTime gameTime) {
            base.Draw(gameTime);
        }

        protected override void OnSelectEntry(int entryIndex) {
            switch (State) {
                case MainMenuState.WELCOME: {
                        switch (entryIndex) {
                            case 0:
                                State = MainMenuState.SELECTLEVEL;
                                break;
                            case 1:
                                State = MainMenuState.OPTIONS;
                                break;
                            case 2:
                                State = MainMenuState.HIGHSCORES;
                                break;
                            case 3:
                                OnCancel();
                                break;
                        }
                    }
                    break;
                case MainMenuState.HIGHSCORES: {
                    switch (entryIndex) {
                        case 0:
                            State = MainMenuState.WELCOME;
                            break;
                        }
                    }
                    break;
                case MainMenuState.SELECTLEVEL: {
                        if (entryIndex == MenuEntries.Count - 1)
                            State = MainMenuState.WELCOME;
                        else {
                            ScreenManager.RemoveScreen(this);
                            LevelScreen level = new LevelScreen();
                            level.trackFolder = Game.Instance.tracks[entryIndex];
                            AudioManager.LoadCustomTrack(level.trackFolder);
                            ScreenManager.AddScreen(level);
                        }
                    }
                    break;
                case MainMenuState.OPTIONS: {
                    switch (entryIndex) {
                        case 0:
                            State = MainMenuState.WELCOME;
                            break;
                        }
                    }
                    break;
            }
        }

        void ExitMessageBoxAccepted(object sender, EventArgs e) {
            ScreenManager.Game.Exit();
        }

        protected override void OnCancel() {
            const string message = "Exit?";
            MessageBoxScreen messageBox = new MessageBoxScreen(message);
            messageBox.Accepted += ExitMessageBoxAccepted;
            ScreenManager.AddScreen(messageBox);
        }
    }
}
